package com.Enquip.control;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.Enquip.entity.User;
 
/**
* <p>Title: UserController</p>
* <p>Description: </p>
* <p>Company: </p>
 * @author douchen
 * @date 2017年12月19日 上午10:40:21
*/
@Controller
public class UserController {

	
	/**
	 *  
	 * @param user
	 * @param request
	 * @return
	 */
	//登录
	@RequestMapping("/login")
	@ResponseBody
	public void login(User user, HttpServletRequest request, HttpServletResponse response) throws Exception {
	 	Subject subject=SecurityUtils.getSubject();
		UsernamePasswordToken token=new UsernamePasswordToken(user.getUserName(), user.getPassword());
		try{
			subject.login(token);
			Session session=subject.getSession();
			session.setAttribute("info", "session 数据");
			response.getWriter().print("success");
		}catch(Exception e){
			e.printStackTrace();
			request.setAttribute("user", user);
			request.setAttribute("errorMsg", "账号或者 密码错误");
			response.getWriter().print("error");
		}
		
		}
	//跳转的  主界面
	@RequestMapping("/index")
	public String toIndex(){
		return "menu";
	}
}
